﻿using KPIS.GERP.Models;
using KPIS.GERP.Models.Class;
using KPIS.GERP.Models.ICM;
using KPIS.GERP.Models.SYS;
using KPIS.GERP.Models.FIM;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using KPIS.GERP.Models.ICM.ViewModel;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic;
using System.ServiceModel;
using System.Web.Mvc;
using System.Collections;

namespace KPIS.GERP.WCF.GIMS
{

    [ServiceContract]
    public interface IPurchaseCheckAssetService
    {
        //#region Asset
        //[OperationContract]
        //IEnumerable<Asset> SelectAssets(Asset criteriaModel = null, List<string> includeSubObjectString = null);

        //[OperationContract]
        //string SelectNameOfficeSuppliesLot(int? Id);

        //[OperationContract]
        //string SelectNameAsset(int? Id);

        //[OperationContract]
        //List<MasResourceItem> SelectNameAndCodeResourceItem(int ResourceItemId);
       
        //#endregion


        //#region InventoryStockTakingItem
        //[OperationContract]
        //IEnumerable<InventoryStockTakingItem> SelectInventoryStockTakingItem(InventoryStockTakingItem criteriaModel = null, List<string> includeSubObjectString = null);

        //[OperationContract]
        //int SelectSumQuanlityAmountStock(int ResItemId, int Status);

        //[OperationContract]
        //IEnumerable<PurchaseCheckAsset> SelectListInventoryStockTakingItem(int? Id, string Status);

        //[OperationContract]
        //string SelectNameMasLookUpByTalkingItem(int ResourceTypeId, int ResItemId);
        //#endregion

        //[OperationContract]
        //IEnumerable<InventoryStockTaking> SelectListInventoryStockTaking(int? Id, string Status);


        [OperationContract]
        List<SelectListItem> SelectListStatus(IcmMasLookUpType.LookUpType lookUpType);

        #region InventoryStockTaking
        [OperationContract]
        IEnumerable<InventoryStockTaking> SelectInventoryStockTaking(InventoryStockTaking criteriaModel = null, List<string> includeSubObjectString = null);

        [OperationContract]
        void UpdateInventoryStockTaking(InventoryStockTaking model, int updateByUserId);

        [OperationContract]
        int InsertInventoryStockTaking(InventoryStockTaking model, int createByUserId);

        [OperationContract]
        void DeleteInventoryStockTaking(int id, int deleteByUserId);

        #endregion


        #region InventoryStockTakingItem

        [OperationContract]
        IEnumerable<InventoryStockTakingItem> SelectInventoryStockTakingItem(InventoryStockTakingItem criteriaModel = null, List<string> includeSubObjectString = null);

        [OperationContract]
        int InsertInventoryStockTakingItem(InventoryStockTakingItem model, int createByUserId);

        [OperationContract]
        void UpdateInventoryStockTakingItem(InventoryStockTakingItem model, int updateByUserId);
        
        #endregion


        #region InventoryStockTakingCommittee

        [OperationContract]
        void Approved(int StockTakingId, int approvedByUserId);

        [OperationContract]
        bool IsCommittee(int StockTakingId);

        #endregion


        #region Asset

        [OperationContract]
        IEnumerable<Asset> SelectAssets(Asset criteriaModel = null, List<string> includeSubObjectString = null);

        // อัพเดทสถานะการใช้งานของครุภัณฑ์
        [OperationContract]
        void UpdateAssetStatus(int ResItemId, int AssetStatusId, int createByUserId);

        #endregion


        #region OfficeSupplyLot

        [OperationContract]
        IEnumerable<OfficeSuppliesLot> SelectOfficeSuppliesLots(OfficeSuppliesLot criteriaModel = null, List<string> includeSubObjectString = null);

        // อัพเดทสถานะการใช้งานของวัสดุ
        [OperationContract]
        void UpdateOfficeSupplyLotStatus(int ResItemId, int OfficeSupplyLotStatusId, int createByUserId);

        #endregion


    }
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "PurchaseCheckAssetService" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select PurchaseCheckAssetService.svc or PurchaseCheckAssetService.svc.cs at the Solution Explorer and start debugging.
    public class PurchaseCheckAssetService : IPurchaseCheckAssetService
    {
        //#region Asset

        //public IEnumerable<Asset> SelectAssets(Asset criteriaModel = null, List<string> includeSubObjectString = null)
        //{
        //    try
        //    {
        //        using (var context = new GERPContext(false))
        //        {
        //            IQueryable<Asset> query = context.Assets;

        //            if (includeSubObjectString != null)
        //            {
        //                query = includeSubObjectString.Aggregate(query, (current, includePath) => current.Include(includePath));
        //            }

        //            if (criteriaModel != null)
        //            {
        //                object[] param;
        //                query = query.Where(KPIS.GERP.Models.Class.Utility.GenerateWhereString(criteriaModel, out param), param);
        //            }

        //            var x = query.ToList();
        //            return x;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //}

        //public List<MasResourceItem> SelectNameAndCodeResourceItem(int ResourceItemId)
        //{
        //    using (GERPContext db = new GERPContext(false))
        //        try
        //        {
        //                var ResourceItem = (from m in db.MasResourceItems
        //                                    where m.ResItemId == ResourceItemId
        //                                    select new
        //                                    {
        //                                        m.Name,
        //                                        m.Code
        //                                    });
        //                List<MasResourceItem> MasResource = new List<MasResourceItem>();
        //                foreach (var m in ResourceItem)
        //                {
        //                    MasResourceItem Resource = new MasResourceItem();
        //                    Resource.Code = m.Code;
        //                    Resource.Name = m.Name;
        //                    MasResource.Add(Resource);
        //                }
        //            return MasResource;

        //        }
        //        catch (Exception ex)
        //        {
        //            throw ex;
        //        }
        //}

        //public string SelectNameAsset(int? Id)
        //{
        //    using (GERPContext db = new GERPContext(false))
        //        try
        //        {
        //            string Tmp = null;
        //            if (Id != 0)
        //            {
        //                string Asset = (from m in db.Assets
        //                                       where m.ResItemId == Id
        //                                       select m.AssetCode).First().ToString();
        //                Tmp = Asset;
        //            }
        //            else
        //            {
        //                Tmp = "";
        //            }
        //            return Tmp;

        //        }
        //        catch (Exception ex)
        //        {
        //            throw ex;
        //        }
        //}

        //public string SelectNameOfficeSuppliesLot(int? Id)
        //{
        //    using (GERPContext db = new GERPContext(false))
        //        try
        //        {
        //            string Tmp = null;
        //            if (Id != 0)
        //            {
        //                string OfficeSuppliesLot = (from m in db.OfficeSuppliesLots
        //                                where m.ResItemId == Id
        //                                select m.LotNo).First().ToString();
        //                Tmp = OfficeSuppliesLot;
        //            }
        //            else
        //            {
        //                Tmp = "";
        //            }
        //            return Tmp;

        //        }
        //        catch (Exception ex)
        //        {
        //            throw ex;
        //        }
        //}

        //#endregion

        //#region InventoryStockTakingItem

        //public IEnumerable<InventoryStockTakingItem> SelectInventoryStockTakingItem(InventoryStockTakingItem criteriaModel = null, List<string> includeSubObjectString = null)
        //{
        //    try
        //    {
        //        using (var context = new GERPContext(false))
        //        {
        //            IQueryable<InventoryStockTakingItem> query = context.InventoryStockTakingItems;

        //            if (includeSubObjectString != null)
        //            {
        //                query = includeSubObjectString.Aggregate(query, (current, includePath) => current.Include(includePath));
        //            }

        //            if (criteriaModel != null)
        //            {
        //                object[] param;
        //                query = query.Where(KPIS.GERP.Models.Class.Utility.GenerateWhereString(criteriaModel, out param), param);
        //            }

        //            var x = query.ToList();
        //            return x;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //}

        //public IEnumerable<PurchaseCheckAsset> SelectListInventoryStockTakingItem(int? Id, string Status)
        //{
        //    try
        //    {
        //        if (Id == 0)
        //        {
        //            using (var context = new GERPContext(false))
        //            {
        //                var query = (from InvenA in context.InventoryStockTakingItems
        //                             join InvenB in context.InventoryStockTakings on InvenA.StockTakingId equals InvenB.StockTakingId into InvenBs
        //                             from InvenB in InvenBs.DefaultIfEmpty().Distinct()
        //                             where InvenA.RecordStatus != Status
        //                             select new
        //                             {
        //                               InvenA.StockTakingItemId,
        //                               InvenA.StockTakingId,
        //                               InvenA.ResItemId,
        //                               InvenA.QuanlityAmountStock,
        //                               InvenA.QuantityAmountReal,
        //                               InvenB.ResourceTypeId
        //                             });
        //                List<PurchaseCheckAsset> Takingitem = new List<PurchaseCheckAsset>();

        //                foreach (var I in query)
        //                {
        //                    PurchaseCheckAsset Item = new PurchaseCheckAsset();
        //                    Item.StockTakingItemIds = I.StockTakingItemId;
        //                    Item.StockTakingIds = I.StockTakingId;
        //                    Item.ResItemIds = I.ResItemId;
        //                    Item.QuanlityAmountStocks = I.QuanlityAmountStock;
        //                    Item.QuantityAmountReals = I.QuantityAmountReal;
        //                    Item.ResourceTypeIds = I.ResourceTypeId;
        //                    Takingitem.Add(Item);
        //                }

        //                return Takingitem;
        //            }
        //        }
        //        else
        //        {
        //            using (var context = new GERPContext(false))
        //            {
        //                var query = (from InvenA in context.InventoryStockTakingItems
        //                             join InvenB in context.InventoryStockTakings on InvenA.StockTakingId equals InvenB.StockTakingId into InvenBs
        //                             from InvenB in InvenBs.DefaultIfEmpty()
        //                             where InvenA.RecordStatus != Status
        //                             && InvenA.StockTakingId == Id
        //                             select new
        //                             {
        //                                 InvenA.StockTakingItemId,
        //                                 InvenA.StockTakingId,
        //                                 InvenA.ResItemId,
        //                                 InvenA.QuanlityAmountStock,
        //                                 InvenA.QuantityAmountReal,
        //                                 InvenB.ResourceTypeId

        //                             });
        //                List<PurchaseCheckAsset> Takingitem = new List<PurchaseCheckAsset>();

        //                foreach (var I in query)
        //                {
        //                    PurchaseCheckAsset Item = new PurchaseCheckAsset();
        //                    Item.StockTakingItemIds = I.StockTakingItemId;
        //                    Item.StockTakingIds = I.StockTakingId;
        //                    Item.ResItemIds = I.ResItemId;
        //                    Item.QuanlityAmountStocks = I.QuanlityAmountStock;
        //                    Item.QuantityAmountReals = I.QuantityAmountReal;
        //                    Item.ResourceTypeIds = I.ResourceTypeId;
        //                    Takingitem.Add(Item);
        //                }

        //                return Takingitem;
        //            }
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //}

        //public int SelectSumQuanlityAmountStock(int ResItemId, int Status)
        //{
        //    using (GERPContext db = new GERPContext(false))
        //        try
        //        {
        //            int Tmp = 0;
        //            if (Status == 39)
        //            {
        //                if (ResItemId == 0)
        //                {
        //                    Tmp = 0;
        //                }
        //                else
        //                {
        //                    int? sums = db.Assets.Where(n => n.ResItemId == ResItemId).Sum(n => n.Quantity);
        //                    if (sums.HasValue) { Tmp = Convert.ToInt32(sums); }
        //                }

        //            }
        //            else
        //            {
        //                if (ResItemId == 0)
        //                {
        //                    Tmp = 0;
        //                }
        //                else
        //                {
        //                    int? sums = db.OfficeSuppliesLots.Where(n => n.ResItemId == ResItemId).Sum(n => n.Quantity);
        //                    if (sums.HasValue) { Tmp = Convert.ToInt32(sums); }
        //                }
        //            }

        //            return Tmp;

        //        }
        //        catch (Exception ex)
        //        {
        //            throw ex;
        //        }
        //}


        //public string SelectNameMasLookUpByTalkingItem(int ResourceTypeId, int ResItemId)
        //{
        //    try
        //    {
        //        using (var context = new GERPContext(false))
        //        {
        //            string Tmp = null;
        //            if (ResourceTypeId == 39)
        //            {
        //                int? id = Convert.ToInt32(context.Assets.Where(a => a.ResItemId == ResItemId).First().AssetTypeId);
        //                string name = context.IcmMasLookUps.Where(i => i.LookUpId == id).First().LookUpName.ToString();
        //                Tmp = name;
        //            }
        //            else if (ResourceTypeId == 40)
        //            {
        //                int? id = Convert.ToInt32(context.OfficeSuppliesLots.Where(a => a.ResItemId == ResItemId).First().OfficeSupplyLotStatusId);
        //                string name = context.IcmMasLookUps.Where(i => i.LookUpId == id).First().LookUpName.ToString();
        //                Tmp = name;
        //            }
        //            else
        //            {
        //                Tmp = "";
        //            }
        //            return Tmp;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //}
        //#endregion
        //public IEnumerable<InventoryStockTaking> SelectListInventoryStockTaking(int? Id, string Status)
        //{
        //    try
        //    {
        //        if (Id == 0)
        //        {
        //            using (var context = new GERPContext(false))
        //            {
        //                IEnumerable<InventoryStockTaking> query = (from Inven in context.InventoryStockTakings
        //                                                           where Inven.RecordStatus != Status
        //                                                           select Inven).ToList();

        //                return query;
        //            }
        //        }
        //        else
        //        {
        //            using (var context = new GERPContext(false))
        //            {
        //                IEnumerable<InventoryStockTaking> query = (from Inven in context.InventoryStockTakings
        //                                                           where Inven.StockTakingId == Id
        //                                                           && Inven.RecordStatus != Status
        //                                                           select Inven).ToList();

        //                return query;
        //            }
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //}


        public List<SelectListItem> SelectListStatus(IcmMasLookUpType.LookUpType lookUpType)
        {
            var data = new List<SelectListItem>();

            var context = new GERPContext(false);
            try
            {
                List<Models.ICM.IcmMasLookUp> listRepairStatus = (from a in context.IcmMasLookUps
                                                                  where a.RecordStatus != Models.SYS.RecordStatus.DeletedStatus
                                                                  && a.LookUpTypeId == (int)lookUpType
                                                                  select a).ToList();

                if (listRepairStatus.Count > 0)
                {
                    foreach (var RepairStatus in listRepairStatus)
                    {
                        var item = new SelectListItem { Text = RepairStatus.LookUpName, Value = RepairStatus.LookUpId.ToString() };
                        data.Add(item);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return data;
        }


        #region InventoryStockTaking

        public IEnumerable<InventoryStockTaking> SelectInventoryStockTaking(InventoryStockTaking criteriaModel = null, List<string> includeSubObjectString = null)
        {
            try
            {
                using (var context = new GERPContext(false))
                {
                    IQueryable<InventoryStockTaking> query = context.InventoryStockTakings;

                    if (includeSubObjectString != null)
                    {
                        query = includeSubObjectString.Aggregate(query, (current, includePath) => current.Include(includePath));
                    }

                    if (criteriaModel != null)
                    {
                        object[] param;
                        query = query.Where(KPIS.GERP.Models.Class.Utility.GenerateWhereString(criteriaModel, out param), param);
                    }

                    var x = query.ToList();
                    return x;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int InsertInventoryStockTaking(InventoryStockTaking model, int createByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    model.CreatedBy = createByUserId;
                    model.StockTakingCode = CodeGenerate.GetNextCode(CodeGenerate.TransectionType.InventoryStockTaking, null);
                    model.CreatedWhen = DateTime.Now;
                    //model.RecordStatus = Models.SYS.RecordStatus.UsedStatus;
                    model.RecordStatus = Models.SYS.RecordStatus.WaitStatus;
                    context.InventoryStockTakings.Add(model);
                    context.SaveChanges();

                    return model.StockTakingId;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void UpdateInventoryStockTaking(InventoryStockTaking model, int updateByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    context.Entry(model).State = EntityState.Modified;
                    model.UpdatedBy = updateByUserId;
                    model.UpdatedWhen = DateTime.Now;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void DeleteInventoryStockTaking(int id, int deleteByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    var modelToDelete = context.InventoryStockTakings.Find(id);
                    if (modelToDelete == null) return;

                    context.Entry(modelToDelete).State = EntityState.Modified;
                    modelToDelete.RecordStatus = RecordStatus.DeletedStatus;
                    modelToDelete.UpdatedBy = deleteByUserId;
                    modelToDelete.UpdatedWhen = DateTime.Now;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        
        #region InventoryStockTakingItem

        public IEnumerable<InventoryStockTakingItem> SelectInventoryStockTakingItem(InventoryStockTakingItem criteriaModel = null, List<string> includeSubObjectString = null)
        {
            try
            {
                using (var context = new GERPContext(false))
                {
                    IQueryable<InventoryStockTakingItem> query = context.InventoryStockTakingItems;

                    if (includeSubObjectString != null)
                    {
                        query = includeSubObjectString.Aggregate(query, (current, includePath) => current.Include(includePath));
                    }

                    if (criteriaModel != null)
                    {
                        object[] param;
                        query = query.Where(KPIS.GERP.Models.Class.Utility.GenerateWhereString(criteriaModel, out param), param);
                    }

                    var x = query.ToList();
                    return x;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int InsertInventoryStockTakingItem(InventoryStockTakingItem model, int createByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    model.CreatedBy = createByUserId;
                    model.CreatedWhen = DateTime.Now;
                    model.RecordStatus = Models.SYS.RecordStatus.WaitStatus;
                    context.InventoryStockTakingItems.Add(model);
                    context.SaveChanges();

                    return model.StockTakingId;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void UpdateInventoryStockTakingItem(InventoryStockTakingItem model, int updateByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    context.Entry(model).State = EntityState.Modified;
                    model.UpdatedBy = updateByUserId;
                    model.UpdatedWhen = DateTime.Now;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion


        #region InventoryStockTakingCommittee

        public void Approved(int StockTakingId, int approvedByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    // Update RecoredStatus to "A" in IcmInventoryStockTaking Table
                    var modelStockTaking = context.InventoryStockTakings.Where(x => x.StockTakingId == StockTakingId).Include("MasResourceType").FirstOrDefault();
                    modelStockTaking.ApprovedBy = approvedByUserId;
                    modelStockTaking.ApprovedWhen = DateTime.Now;
                    modelStockTaking.RecordStatus = Models.SYS.RecordStatus.ApprovedStatus;
                    context.Entry(modelStockTaking).State = EntityState.Modified;
                    context.SaveChanges();

                    // Update RecoredStatus to "A" in IcmInventoryStockTakingItem Table
                    var modelStockTakingItem = context.InventoryStockTakingItems.Where(x => x.StockTakingId == StockTakingId).Include("MasResourceItem").ToList();
                    
                    var ResourceItemId = modelStockTakingItem.Select(x=>x.ResItemId).ToList();

                    foreach (var m in modelStockTakingItem )
                    {
                        m.UpdatedBy = approvedByUserId;
                        m.UpdatedWhen = DateTime.Now;
                        m.RecordStatus = Models.SYS.RecordStatus.ApprovedStatus;
                        context.Entry(m).State = EntityState.Modified;
                        context.SaveChanges();
                    }

                    // ประเภทพัสดุ
                    string ResourceType = modelStockTaking.MasResourceType.Name;

                    // เช็คว่าประเภทพัสดุว่าเป็นวัสดุ หรือ ครุภัณฑ์ 
                    // ถ้าเป็น "วัสดุ" ก็จะไปอัพเดทฟิล์ด OfficeSupplyLotStatusId ในตาราง IcmOfficeSuppliesLot
                    // แต่ถ้าเป็น "ครุภัณฑ์" ก็จะไปอัพเดทฟิล์ด AssetStatusId ในตาราง IcmAsset
                    if (ResourceType.Equals("วัสดุ"))
                    {
                        foreach (var m in ResourceItemId)
                        {
                            var modelOfficeSupplyLot = context.OfficeSuppliesLots.Where(x => x.ResItemId == m).ToList();
                            int StatusId = modelStockTakingItem.Where(x => x.ResItemId == m).Select(x => x.StatusParcel).FirstOrDefault();

                            if (modelOfficeSupplyLot != null)
                            {
                                for (int i = 0; i < modelOfficeSupplyLot.Count; i++ )
                                {
                                    // Update OfficeSupplyLotStatusId in IcmOfficeSuppliesLot
                                    var model = modelOfficeSupplyLot[i];
                                    model.OfficeSupplyLotStatusId = StatusId;
                                    model.UpdatedBy = approvedByUserId;
                                    model.UpdatedWhen = DateTime.Now;
                                    context.Entry(model).State = EntityState.Modified;
                                    context.SaveChanges();
                                }
                            }
                        }                        
                    }
                    else if (ResourceType.Equals("ครุภัณฑ์"))
                    {
                        foreach (var m in ResourceItemId)
                        {
                            var modelAsset = context.Assets.Where(x => x.ResItemId == m).ToList();
                            int StatusId = modelStockTakingItem.Where(x => x.ResItemId == m).Select(x => x.StatusParcel).FirstOrDefault();

                            if (modelAsset != null)
                            {
                                for (int i = 0; i < modelAsset.Count; i++)
                                {
                                    // Update AssetStatusId in IcmAssets
                                    var model = modelAsset[i];
                                    model.AssetStatusId = StatusId;
                                    model.UpdatedBy = approvedByUserId;
                                    model.UpdatedWhen = DateTime.Now;
                                    context.Entry(model).State = EntityState.Modified;
                                    context.SaveChanges();
                                }
                            }
                        }
                    }

                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        // ตรวจสอบว่าใบตรวจนับแต่ละใบว่ามี "คณะกรรมการตรวจนับ" แล้วหรือยัง
        public bool IsCommittee(int StockTakingId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    bool IsCommittee = false;

                    // ComFunctionId == 76  >>>   76 คือ คณะกรรมการตรวจนับ   มาจาก LookUpId ในตาราง IcmMasLookup 
                    // var model = context.Committees.Where(x => x.RefId == StockTakingId && x.ComFunctionId == 76).ToList();
                    var model = context.Committees.Where(x => x.CommitteeGroupId == StockTakingId && x.ComFunctionId == 76).ToList();

                    if (model.Count > 0)
                    {
                        IsCommittee = true;
                    }

                    return IsCommittee;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        #endregion

        #region Asset

        public IEnumerable<Asset> SelectAssets(Asset criteriaModel = null, List<string> includeSubObjectString = null)
        {
            try
            {
                using (var context = new GERPContext(false))
                {
                    IQueryable<Asset> query = context.Assets;

                    if (includeSubObjectString != null)
                    {
                        query = includeSubObjectString.Aggregate(query, (current, includePath) => current.Include(includePath));
                    }

                    if (criteriaModel != null)
                    {
                        object[] param;
                        query = query.Where(KPIS.GERP.Models.Class.Utility.GenerateWhereString(criteriaModel, out param), param);
                    }

                    var x = query.ToList();
                    return x;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        // อัพเดทสถานะการใช้งานของครุภัณฑ์
        public void UpdateAssetStatus(int ResItemId, int AssetStatusId, int createByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    var model = context.Assets.Where(x => x.ResItemId == ResItemId).FirstOrDefault();
                    model.AssetStatusId = AssetStatusId;
                    model.UpdatedBy = createByUserId;
                    model.UpdatedWhen = DateTime.Now;
                    context.Assets.Add(model);
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion


        #region OfficeSupplyLot

        public IEnumerable<OfficeSuppliesLot> SelectOfficeSuppliesLots(OfficeSuppliesLot criteriaModel = null, List<string> includeSubObjectString = null)
        {
            try
            {
                using (var context = new GERPContext(false))
                {
                    IQueryable<OfficeSuppliesLot> query = context.OfficeSuppliesLots;

                    if (includeSubObjectString != null)
                    {
                        query = includeSubObjectString.Aggregate(query, (current, includePath) => current.Include(includePath));
                    }

                    if (criteriaModel != null)
                    {
                        object[] param;
                        query = query.Where(KPIS.GERP.Models.Class.Utility.GenerateWhereString(criteriaModel, out param), param);
                    }

                    var x = query.ToList();
                    return x;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        // อัพเดทสถานะการใช้งานของวัสดุ
        public void UpdateOfficeSupplyLotStatus(int ResItemId, int OfficeSupplyLotStatusId, int createByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    var model = context.OfficeSuppliesLots.Where(x => x.ResItemId == ResItemId).FirstOrDefault();
                    model.OfficeSupplyLotStatusId = OfficeSupplyLotStatusId;
                    model.UpdatedBy = createByUserId;
                    model.UpdatedWhen = DateTime.Now;
                    context.OfficeSuppliesLots.Add(model);
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion









    }

}
